package com.minivision.sdktestdemo.model;

import android.content.Context;
import android.net.Uri;

import com.github.promeg.pinyinhelper.Pinyin;
import com.minivision.sdktestdemo.bean.KeyBean;
import com.minivision.sdktestdemo.bean.MusicBean;
import com.minivision.sdktestdemo.bean.UriOrderGetter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.TreeMap;

/**
 * Created by devtang on 2016/4/8.
 */
public abstract class Provider<T> {
    protected List<T> mList;

    private boolean mIsChanged;

    protected Provider() {
        mList = new ArrayList<T>();
    }

    public T get(int position) {
        return mList.get(position);
    }

    public int size() {
        return mList.size();
    }

    public boolean isChanged() {
        return mIsChanged;
    }

    public void setChanged(boolean flag) {
        mIsChanged = flag;
    }

    public abstract int remove(T data, boolean isDeletedFlag);

    public abstract String getKey(MusicProvider provider, int position);

    List<KeyBean> getClassfiedList(MusicProvider provider) {
        //先统计每一个类别包括哪些歌曲
        TreeMap<String, List<MusicBean>> map = new TreeMap<String, List<MusicBean>>();
        for (int i = 0; i < provider.size(); ++i) {
            List<MusicBean> list = map.get(getKey(provider, i)) == null ?
                    new ArrayList<MusicBean>() : map.get(getKey(provider, i));
            list.add(provider.get(i));
            map.put(getKey(provider, i), list);
        }
        //然后获取所有统计的类别
        List<KeyBean> list = new ArrayList<KeyBean>();
        for (String key : map.keySet()) {
            Uri uri = map.get(key).iterator().next().getmPicUri();
            list.add(new KeyBean(key, map.get(key), uri));
        }
        return list;
    }

    public List<T> getList() {
        return mList;
    }

    public abstract void update();

    public void sort(List<T> list) {
        if (list == null) {
            list = mList;
        }
        Collections.sort(list, new Comparator<T>() {
            @Override
            public int compare(T lhs, T rhs) {
                UriOrderGetter lGetter = (UriOrderGetter) lhs;
                UriOrderGetter rGetter = (UriOrderGetter) rhs;
                String lKey = Pinyin.toPinyin(lGetter.getKey().charAt(0));
                String rKey = Pinyin.toPinyin(rGetter.getKey().charAt(0));
                return lKey.compareTo(rKey);
            }
        });
    }

    public static void adjustGroup(Context context, MusicBean bean) {
        AlbumProvider.getInstance(MusicProvider.getInstance(context)).remove(bean);
        DirPathProvider.getInstance(MusicProvider.getInstance(context)).remove(bean);
        SingerProvider.getInstance(MusicProvider.getInstance(context)).remove(bean);
    }

    public static void adjustGroup(Context context, List<MusicBean> beans) {
        AlbumProvider.getInstance(MusicProvider.getInstance(context)).remove(beans);
        DirPathProvider.getInstance(MusicProvider.getInstance(context)).remove(beans);
        SingerProvider.getInstance(MusicProvider.getInstance(context)).remove(beans);
    }

    public static void release() {
        MusicProvider.release();
        AlbumProvider.release();
        DirPathProvider.release();
        SingerProvider.release();
    }
//
//    public static void setAllChanged(Context context) {
//        MusicProvider provider = MusicProvider.getInstance(context);
//        provider.setChanged(true);
//        AlbumProvider.getInstance(provider).setChanged(true);
//        DirPathProvider.getInstance(provider).setChanged(true);
//        SingerProvider.getInstance(provider).setChanged(true);
//    }

    public static void setAllChanged(MusicProvider provider) {
        provider.setChanged(true);
        AlbumProvider.getInstance(provider).setChanged(true);
        DirPathProvider.getInstance(provider).setChanged(true);
        SingerProvider.getInstance(provider).setChanged(true);
    }
}
